package com.cloud.flowable.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.flowable.entity.WfCopy;
import com.cloud.flowable.entity.bo.ProcessPage;
import com.cloud.entity.vo.TaskParam;
import com.cloud.flowable.entity.vo.WfDefinitionVo;
import com.cloud.flowable.entity.vo.WfTaskVo;
import com.cloud.flowable.service.IWfCopyService;
import com.cloud.flowable.service.IWfProcessService;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.utils.ExcelPoiUtils;
import com.cloud.utils.SecurityUtils;
import com.cloud.utils.ServletUtils;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "工作流任务管理")
@RestController
@RequestMapping("/flowable/process")
public class WfProcessController extends BaseController {

    @Autowired
    private IWfProcessService processService;

    @Autowired
    private IWfCopyService copyService;

    @Operation(summary = "新建流程列表")
    @GetMapping(value = "/list")
    @RequestPermissions("flowable:process:startList")
    public Result startProcessList(ProcessPage procPage) {
        return Result.success("查询成功", processService.selectStartProcessPage(procPage));
    }

    @Operation(summary = "我拥有的流程")
    @RequestPermissions("flowable:process:ownList")
    @GetMapping(value = "/ownList")
    public Result ownProcessList(ProcessPage procPage) {
        return Result.success("查询成功", processService.selectOwnProcessPage(procPage));
    }

    @Operation(summary = "获取待办列表")
    @RequestPermissions("flowable:process:todoList")
    @GetMapping(value = "/todoList")
    public Result todoProcessList(ProcessPage procPage) {
        return Result.success("查询成功", processService.selectTodoProcessPage(procPage));
    }

    @Operation(summary = "获取待签列表")
    @RequestPermissions("flowable:process:claimList")
    @GetMapping(value = "/claimList")
    public Result claimProcessList(ProcessPage procPage) {
        return Result.success("查询成功", processService.selectClaimProcessPage(procPage));
    }

    @Operation(summary = "获取已办列表")
    @RequestPermissions("flowable:process:finishedList")
    @GetMapping(value = "/finishedList")
    public Result finishedProcessList(ProcessPage procPage) {
        return Result.success("查询成功", processService.selectFinishedProcessPage(procPage));
    }

    @Operation(summary = "获取抄送列表")
    @RequestPermissions("flowable:process:copyList")
    @GetMapping(value = "/copyList")
    public Result copyProcessList(ProcessPage procPage) {
        startPage();
        procPage.setUserId(SecurityUtils.getUserId());
        List<WfCopy> copyList = copyService.selectList(procPage);
        return getDataTable(copyList);
    }

    @Operation(summary = "导出可发起流程列表")
    @RequestPermissions("flowable:process:startExport")
    @Logs(title = "可发起流程", businessType = BusinessType.EXPORT)
    @GetMapping("/startExport")
    public void startExport(@Validated ProcessPage procPage) {
        List<WfDefinitionVo> list = processService.selectStartProcessList(procPage);
        String titleName = "可发起流程";
        ExcelPoiUtils.exportExcel(list, titleName, "可发起流程",
                WfDefinitionVo.class, titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "导出我拥有流程列表")
    @RequestPermissions("flowable:process:ownExport")
    @Logs(title = "我拥有流程", businessType = BusinessType.EXPORT)
    @GetMapping("/ownExport")
    public void ownExport(@Validated ProcessPage procPage) {
        List<WfTaskVo> list = processService.selectOwnProcessList(procPage);
        for (WfTaskVo taskVo : list) {
            taskVo.setProcessStatus(ObjectUtil.isNull(taskVo.getFinishTime()) ? "进行中" : "已完成");
        }
        String titleName = "我拥有流程";
        ExcelPoiUtils.exportExcel(list, titleName, "我拥有流程",
                WfTaskVo.class, titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "导出待办流程列表")
    @RequestPermissions("flowable:process:todoExport")
    @Logs(title = "待办流程", businessType = BusinessType.EXPORT)
    @GetMapping("/todoExport")
    public void todoExport(@Validated ProcessPage procPage) {
        List<WfTaskVo> list = processService.selectTodoProcessList(procPage);
        String titleName = "待办流程";
        ExcelPoiUtils.exportExcel(list, titleName, "待办流程",
                WfTaskVo.class, titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "导出待签流程列表")
    @RequestPermissions("flowable:process:claimExport")
    @Logs(title = "待签流程", businessType = BusinessType.EXPORT)
    @GetMapping("/claimExport")
    public void claimExport(@Validated ProcessPage procPage) {
        List<WfTaskVo> list = processService.selectClaimProcessList(procPage);
        String titleName = "待签流程";
        ExcelPoiUtils.exportExcel(list, titleName, "待签流程",
                WfTaskVo.class, titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "导出已办流程列表")
    @RequestPermissions("flowable:process:finishedExport")
    @Logs(title = "已办流程", businessType = BusinessType.EXPORT)
    @GetMapping("/finishedExport")
    public void finishedExport(@Validated ProcessPage procPage) {
        List<WfTaskVo> list = processService.selectFinishedProcessList(procPage);
        String titleName = "已办流程";
        ExcelPoiUtils.exportExcel(list, titleName, "已办流程",
                WfTaskVo.class, titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "导出抄送流程列表")
    @RequestPermissions("flowable:process:copyExport")
    @Logs(title = "抄送流程", businessType = BusinessType.EXPORT)
    @GetMapping("/copyExport")
    public void copyExport(@Validated ProcessPage procPage) {
        procPage.setUserId(SecurityUtils.getUserId());
        List<WfCopy> copyList = copyService.selectList(procPage);
        String titleName = "抄送流程";
        ExcelPoiUtils.exportExcel(copyList, titleName, "抄送流程",
                WfCopy.class, titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "查询流程部署关联表单信息")
    @RequestPermissions("flowable:process:start")
    @GetMapping("/getProcessForm")
    public Result getForm(String definitionId, String deployId) {
        return Result.success("查询成功", processService.selectFormContent(definitionId, deployId));
    }

    @Operation(summary = "根据流程ID启动流程实例")
    @RequestPermissions("flowable:process:start")
    @PostMapping("/startProcId")
    public Result startProcId(@RequestBody TaskParam taskParam) {
        processService.startProcByDefId(taskParam);
        return Result.success("操作成功");
    }

    @Operation(summary = "删除流程实例")
    @RequestPermissions("flowable:process:remove")
    @PostMapping("/remove")
    public Result remove(String instanceIds) {
        List<String> ids = ToolUtils.setStrToList(instanceIds);
        processService.deleteProcessByIds(ids);
        return Result.success("操作成功");
    }

    @Operation(summary = "读取xml文件")
    @GetMapping("/bpmnXml")
    public Result getBpmnXml(String procDefId) {
        return Result.success("查询成功", processService.queryBpmnXmlById(procDefId));
    }

    @Operation(summary = "查询流程详情信息")
//    @RequestPermissions("flowable:process:detail")
    @GetMapping("/detail")
    public Result detail(String procInsId, String taskId) {
        return Result.success("查询成功", processService.queryProcessDetail(procInsId, taskId));
    }

    @Operation(summary = "根据流程key启动流程实例")
    @PostMapping("/startProcKey")
    public Result startProcKey(@RequestBody TaskParam taskParam) {
        return Result.success("操作成功", processService.startProcByDefKey(taskParam));
    }

}
